package gold.digger;

import java.util.*;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC299 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        // xA 表示有 x 位数字出现在秘密数字中，且位置都与秘密数字一致。
        // yB 表示有 y 位数字出现在秘密数字中，但位置与秘密数字不一致。
        public String getHint(String secret, String guess) {
            int aCount = 0, bCount = 0;
            Map<Character, Integer> existMap = new HashMap<>();
            Set<Integer> ignorePos = new HashSet<>();
            for (int i = 0; i < secret.length(); i++) {
                char x = secret.charAt(i), y = guess.charAt(i);
                if (x == y) {
                    ++aCount;
                    ignorePos.add(i);
                    continue;
                }
                existMap.put(x, existMap.getOrDefault(x, 0) + 1);
            }
            for (int i = 0; i < guess.length(); i++) {
                if (ignorePos.contains(i)) continue;
                char y = guess.charAt(i);
                if (existMap.containsKey(y)) {
                    ++bCount;
                    if (existMap.get(y) == 1) existMap.remove(y);
                    else existMap.put(y, existMap.get(y) - 1);
                }
            }

            return aCount + "A" + bCount + "B";
        }
    }

    public void run() {
        Solution solution = new Solution();
        System.out.println(solution.getHint("1123", "0111"));
        System.out.println(solution.getHint("11", "10"));
        System.out.println(solution.getHint("1122", "2211"));
    }

    public static void main(String[] args) throws Exception {
        LC299 an = new LC299();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
